package edu.whu.demo.controller;


import edu.whu.demo.domain.Commodity;
import edu.whu.demo.service.ICommodityService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王冠儒
 * @since 2023-10-22
 */
@RestController
@RequestMapping("/commodity")
public class CommodityController {

    @Resource
    private ICommodityService commodityService;

    @GetMapping("")
    public ResponseEntity<List<Commodity>> findAllCommodity(){
        return ResponseEntity.ok(commodityService.list());
    }

    @GetMapping("{commodityId}")
    public ResponseEntity<Commodity> findCommodityById(String commodityId){
        return ResponseEntity.ok(commodityService.getById(commodityId));
    }

    @PostMapping("")
    @PreAuthorize("hasAuthority('commodity/update')")
    public ResponseEntity<Void> addCommodity(@RequestBody Commodity commodity){
        commodityService.save(commodity);
        return ResponseEntity.ok().build();
    }

}

